package controllers;
import business.ApiService;
import play.Logger;
import utils.Convert;
import models.*;
import utils.*;
import java.util.*;

/**
 * 后台登录, 增删改 统一用 ajax, 查用 回发
 */
public class BackendCenter extends  BaseController{
    //信息列表
    public static void newsList(String title,long channel_id){
        PageBean<cms_news> pageBean = ApiService.getNewsList(title,channel_id);
        Logger.info(pageBean.page.size()+"");
        List<cms_channel> channelList = ApiService.getChannelList();
        render(pageBean,channelList,channel_id,title);
    }
    //信息编辑
    public static void newsEdit(String v,String title,long id,String content,long read_count,String filename,long channel_id,String summary){
        ErrorInfo errorInfo = new ErrorInfo();
        cms_news item;
        if("add".equals(v)){
            if(id!=0){
                item = cms_news.findById(id);
            }else{
                item = new cms_news();
                item.created_date = new Date();
            }
            item.channel_id = channel_id;
            item.title = title;
            item.content = content;
            item.filename = filename;
            item.read_count = read_count;
            item.modify_date = new Date();
            item.summary = summary;
            cms_channel channel = cms_channel.findById(channel_id);
            item.channel_name = channel.channel_name;
            item.save();
            errorInfo.initSave();
            renderJSON(errorInfo);
        }else if("edit".equals(v)){
            item = cms_news.findById(Long.parseLong(params.get("id")));
            List<cms_channel> channelList = ApiService.getChannelList();
            render(item,channelList);
        }else if("show".equals(v)){
            List<cms_channel> channelList = ApiService.getChannelList();
            render(channelList);
        }
        renderText("error access");
    }
    // 信息删除
    public static void newsDel(String ids){
        ErrorInfo errorInfo = new ErrorInfo();
        String[] strs=ids.split(",");
        for(int i=0;i<strs.length;i++){
            long idL = Convert.strToLong(strs[i],0);
            if(idL>0){
                cms_news.delete("from cms_news where id=?1",idL);
            }
        }
        errorInfo.msg="删除成功";
        errorInfo.code=1;
        renderJSON(errorInfo);
    }
}
